package cn.iocoder.yudao.module.system.repository.mybatis.dao.errorcode;

import cn.iocoder.yudao.common.pojo.PageResult;
import cn.iocoder.yudao.core.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.core.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.repository.mybatis.domain.errorcode.ErrorCode;
import cn.iocoder.yudao.module.system.vo.errorcode.ErrorCodeExportReqVO;
import cn.iocoder.yudao.module.system.vo.errorcode.ErrorCodePageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Mapper
public interface ErrorCodeMapper extends BaseMapperX<ErrorCode> {

    default PageResult<ErrorCode> selectPage(ErrorCodePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErrorCode>()
                .eqIfPresent(ErrorCode::getType, reqVO.getType())
                .likeIfPresent(ErrorCode::getApplicationName, reqVO.getApplicationName())
                .eqIfPresent(ErrorCode::getCode, reqVO.getCode())
                .likeIfPresent(ErrorCode::getMessage, reqVO.getMessage())
                .betweenIfPresent(ErrorCode::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ErrorCode::getCode));
    }

    default List<ErrorCode> selectList(ErrorCodeExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ErrorCode>()
                .eqIfPresent(ErrorCode::getType, reqVO.getType())
                .likeIfPresent(ErrorCode::getApplicationName, reqVO.getApplicationName())
                .eqIfPresent(ErrorCode::getCode, reqVO.getCode())
                .likeIfPresent(ErrorCode::getMessage, reqVO.getMessage())
                .betweenIfPresent(ErrorCode::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ErrorCode::getCode));
    }

    default List<ErrorCode> selectListByCodes(Collection<Integer> codes) {
        return selectList(ErrorCode::getCode, codes);
    }

    default ErrorCode selectByCode(Integer code) {
        return selectOne(ErrorCode::getCode, code);
    }

    default List<ErrorCode> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) {
        return selectList(new LambdaQueryWrapperX<ErrorCode>().eq(ErrorCode::getApplicationName, applicationName)
                .gtIfPresent(ErrorCode::getUpdateTime, minUpdateTime));
    }

}
